﻿using System;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using AB.ServiceDirectory.Commons.DataFilters;

namespace AB.ServiceDirectory.Design.HContact
{
    public partial class ContactList02 : Page
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                var filter = new HContactFilter();
                ViewState["filter"] = filter;
                TextBoxFirstName.Focus();
            }
        }

        /// <summary>
        /// Handles the Click event of the ButtonSearchContact control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonSearchContactClick(object sender, EventArgs e)
        {
            string firstname = TextBoxFirstName.Text;
            string surname = TextBoxName.Text;

            var filter = ViewState["filter"] as HContactFilter;
            if (filter != null)
            {
                filter.FirstName = firstname.Trim();
                filter.Surname = surname.Trim();
                GridViewContact.DataBind();
            }
        }

        /// <summary>
        /// Handles the CheckedChanged event of the CheckBoxInactive control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void CheckBoxInactiveCheckedChanged(object sender, EventArgs e)
        {
            var filter = ViewState["filter"] as HContactFilter;
            if (filter != null)
            {
                filter.IncludeInactiveContact = CheckBoxInactive.Checked;
                GridViewContact.DataBind();
            }
        }

        /// <summary>
        /// Handles the Click event of the ButtonClear control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonClearClick(object sender, EventArgs e)
        {
            TextBoxFirstName.Text = string.Empty;
            TextBoxName.Text = string.Empty;
        }

        /// <summary>
        /// Handles the Click event of the ButtonCreate control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonCreateClick(object sender, EventArgs e)
        {
            Response.Redirect("~/Contact_HungND12/AddContact.aspx");
        }

        /// <summary>
        /// Buttons the edit click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonEditClick(object sender, EventArgs e)
        {
            if (Request.Form["MyRadioButton"] == null)
            {
                return;
            }

            int selectedValue = int.Parse(Request.Form["MyRadioButton"], CultureInfo.CurrentCulture);
            Response.Redirect("editcontact.aspx?contactId=" + selectedValue);
        }

        /// <summary>
        /// Contacts the object data source selecting.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs"/> instance containing the event data.</param>
        protected void ContactObjectDataSourceSelecting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            var filter = ViewState["filter"] as HContactFilter;
            e.InputParameters["filter"] = filter;
        }
    }
}